package com.eric.windows.resource;

import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.eric.windows.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.DrawableRes;

import java.util.Timer;
import java.util.TimerTask;

/**
 * <h3>Description</h3> android  资源测试
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/12 14:54
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */
@EActivity(R.layout.activity_res_test)
public class ResTestActivity extends Activity {
    @ViewById(R.id.img_res_shape)
    ImageView  imgShape;
    @DrawableRes(R.drawable.res_shape_drawable)
    Drawable drawableShape;

    /**通过注解查找控件 */
    @ViewById(R.id.img_res_clip)
    ImageView  imgClip;
    /**通过注解查找资源图片 需要导包  可使用getDrawable(R.drawable.res_clip_drawable)获取*/
    @DrawableRes(R.drawable.res_clip_drawable)
    Drawable drawableClip;
    /** 计时器 每隔一段时间发送一个message 实现图片徐徐展开*/
    private Timer  timer = new Timer();
    private ClipDrawable clipDrawable;
    private Handler  handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 21:
                    if (drawableClip.getLevel() > 10000 ){
                        drawableClip.setLevel(0);
                    }
                    //徐徐展开图片
                    drawableClip.setLevel(drawableClip.getLevel() + 200);
                    break;
            }

        }
    };



    /** 初始化组件 */
    @AfterViews
    void initViews(){
//        drawableShape = getDrawable(R.drawable.res_shape_drawable);
        LayerDrawable  layerDrawable  = (LayerDrawable) getDrawable(R.drawable.res_layer_drawable);

        imgShape.setImageDrawable(drawableShape);
        clipDrawable = (ClipDrawable) drawableClip;
        imgClip.setImageDrawable(clipDrawable);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(21);
            }
        },0,300);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null){
            timer.cancel();
            timer = null;
        }
    }
}
